package com.cysd.ewmxns.sdw_ui.dialog;
import android.animation.AnimatorSet;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.cszy.ewmxns.common.AppConstants;

import com.cysd.ewmxns.sdw_ui.activity.Sdw_HtmlActivity;
import com.cysd.ewmxns.sdw_utils.SpUtils;
import com.hlyj.http.ad.NewGroMoreFeedAdManager;
import com.hlyj.http.base.tool.lib_hlyj_base.utils.BaseAppConstans;
import com.hnyyac.ad.mds.utils.BaseAdConstants;
import qrrspeeelild.rqrc0ode.andro0ild.R;


public class Sdw_UserAgreementDialog extends BaseDialog {
    private Context mContent;
    private TextView txt_privteId;
    private TextView txt_noAgree;
    private TextView txt_Agree;
    private TextView txt_xieyi;
    private TextView txt_yinsi;
    private RelativeLayout mAgree_agreement;
    private CheckBox mAgree_check;
    public AgreementDialogListner agreementDialogListner;
    private FrameLayout frame_content;
    private NewGroMoreFeedAdManager newGromoreFeedAd;
    public Sdw_UserAgreementDialog(Context context) {
        super(context);
        this.mContent = context;
    }

    @Override
    protected float setWidthScale() {
        return 1.0f;
    }

    @Override
    protected AnimatorSet setEnterAnim() {
        return null;
    }

    @Override
    protected AnimatorSet setExitAnim() {
        return null;
    }

    @Override
    protected void init() {
        frame_content=findViewById(R.id.frame_content);
        initAdAfterLayoutMeasured();
        // checkBox = (CheckBox) findViewById(R.id.xieyi_checkbox);
        txt_xieyi = findViewById(R.id.txt_xieyi);
        txt_yinsi = findViewById(R.id.txt_yinsi);
        txt_privteId=findViewById(R.id.txt_private);
        txt_Agree=findViewById(R.id.txt_Agree);
        txt_noAgree=findViewById(R.id.txt_NoAgree);
        mAgree_agreement=findViewById(R.id.mAgree_agreement);
        mAgree_check=findViewById(R.id.mAgree_check);

        mAgree_agreement.setOnClickListener(view -> {
            if(!mAgree_check.isChecked()){
                mAgree_check .setChecked(!mAgree_check.isChecked());
            }
            dismiss();
            SpUtils.getInstance().putBoolean(BaseAppConstans.isAgreeMent, true);
            agreementDialogListner.onDialogListener(true);
        });

        //第一种弹窗
        if(false){
            mAgree_agreement.setVisibility(View.GONE);
            txt_Agree.setVisibility(View.VISIBLE);
        }else {
            //第二种弹窗
            mAgree_agreement.setVisibility(View.VISIBLE);
            txt_Agree.setVisibility(View.GONE);
        }

        txt_xieyi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, Sdw_HtmlActivity.class);
                intent.putExtra("url", AppConstants.User_Agreement);
                intent.putExtra("title", "用户协议");
                mContext.startActivity(intent);

            }
        });


        txt_yinsi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, Sdw_HtmlActivity.class);
                intent.putExtra("url", AppConstants.Privacy_Policy);
                intent.putExtra("title", "隐私政策");
                mContext.startActivity(intent);

            }
        });

        txt_Agree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                SpUtils.getInstance().putBoolean(BaseAppConstans.isAgreeMent, true);
                agreementDialogListner.onDialogListener(true);


            }
        });
//
        txt_noAgree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                agreementDialogListner.onDialogListener(false);
            }
        });


    }
    /**
     * 在 Dialog 布局完成后初始化广告，并监听高度变化
     */
    private void initAdAfterLayoutMeasured() {
        final View decorView = getWindow().getDecorView();
        decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // 移除监听，避免重复调用
                decorView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                // 获取 Dialog 的实际宽度
                int dialogWidth = decorView.getWidth();
                Log.d("FunctionGuideDialog", "Dialog宽度: " + dialogWidth);

                // 初始化广告（确保宽度有效）
                if (mContext instanceof Activity) {
                        newGromoreFeedAd = new NewGroMoreFeedAdManager(
                                (Activity) mContext,
                                BaseAdConstants.FEEDAD_Id,
                                frame_content,
                                8
                        );
                 // 设置广告宽度（减去左右 margin 10dp）
                    int adWidth = frame_content.getWidth();
                    int adHeight = frame_content.getHeight(); // 默认高度（可根据需求调整）
                    newGromoreFeedAd.setFeedAdWidth(adWidth, adHeight);
                    // 加载广告
                    newGromoreFeedAd.loadAd();
                    // 监听广告容器高度变化
                    setupAdHeightListener();
                } else {
                    Log.e("FunctionGuideDialog", "必须使用 Activity 的 Context");
                }
            }
        });
    }
    /**
     * 监听广告容器高度变化，动态调整 Dialog 高度
     */
    private void setupAdHeightListener() {
        frame_content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int adHeight = frame_content.getHeight();
                if (adHeight > 0) {
                    // 动态更新 Dialog 的 Window 高度
                    Window window = getWindow();
                    if (window != null) {
                        WindowManager.LayoutParams params = window.getAttributes();
                        params.height = ViewGroup.LayoutParams.WRAP_CONTENT; // 或固定值
                        window.setAttributes(params);
                    }
                }
            }
        });
    }
    public void setPrivateId(String privateId){
        txt_privteId.setText(privateId);
    }
    @Override
    protected int getContentViewId() {
        return R.layout.dialog_useragreement;
    }

    public void setOnAgreementListener(AgreementDialogListner listener) {
        this.agreementDialogListner = listener;
    }


    public interface AgreementDialogListner {
        void onDialogListener(Boolean clickType);
    }
}
